package com.css.fxfzypg.modules.baseData.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.baseData.queryParams.HosPeoQueryParams;
import com.css.fxfzypg.modules.baseData.entity.HosPeoEntity;
import com.css.fxfzypg.modules.baseData.queryParams.VillageTownHosPeoQueryParams;
import com.css.fxfzypg.modules.baseData.service.HosPeoService;
import com.css.fxfzypg.modules.baseData.service.VillageTownHosPeoService;
import com.css.fxfzypg.util.PlatformPageUtils;
import com.css.fxfzypg.util.ResponseCreator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author y1fan
 * @create 2022-08-23 11:33
 * 人口和建筑物概况子模块
 */
@RestController
@RequestMapping("/fxfzypg/baseData")
@Slf4j
public class HosPeoController {

    @Resource
    private HosPeoService hosPeoService;

    @Resource
    private VillageTownHosPeoService villageTownHosPeoService;

    @PostMapping("/saveHosPeo")
    public RestResponse saveHosPeo(@RequestBody HosPeoEntity hosPeoEntity){
        RestResponse restResponse = null;
        try {
            restResponse = hosPeoService.saveHosPeo(hosPeoEntity);
            // restResponse = RestResponse.fail("功能暂时缺失");
        } catch (Exception e) {
            log.error("添加人口和建筑物概况失败",e);
            restResponse = RestResponse.fail("添加人口和建筑物概况失败");
        }
        return restResponse;
    }

    @GetMapping("/getHosPeoById/{id}")
    public RestResponse getHosPeoById(@PathVariable("id")String id){
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(hosPeoService.fingHosPeoById(id));
        } catch (Exception e) {
            log.error("查询人口和建筑物概况失败",e);
            restResponse =   RestResponse.fail("查询人口和建筑物概况失败");
        }
        return restResponse;
    }

    @PostMapping("/updateHosPeo")
    public RestResponse updateHosPeo(@RequestBody HosPeoEntity hosPeoEntity){
        RestResponse restResponse = null;
        try {
            restResponse = hosPeoService.updateHosPeo(hosPeoEntity);
        } catch (Exception e) {
            log.error("修改人口和建筑物概况失败",e);
            restResponse =   RestResponse.fail("修改人口和建筑物概况失败");
        }
        return restResponse;
    }

    @GetMapping("/getAllHosPeo")
    public RestResponse getAllHisPr(HttpServletRequest request, HosPeoQueryParams queryParams){
        RestResponse restResponse = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            JSONObject jsonObject = hosPeoService.getAllHosPeoByParams(queryParams, curPage, pageSize);
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            log.error("查询人口和建筑物概况失败!", e);
            restResponse = RestResponse.fail("查询人口和建筑物概况失败");
        }
        return restResponse;
    }

//    @GetMapping("/getAllHosPeo")
//    public RestResponse getAllHisPr(HttpServletRequest request, VillageTownHosPeoQueryParams queryParams){
//        RestResponse restResponse = null;
//        try {
//            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
//            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
//            JSONObject jsonObject = villageTownHosPeoService.getAllHosPeoByParams(queryParams, curPage, pageSize);
//            restResponse = RestResponse.succeed(jsonObject);
//        } catch (Exception e) {
//            log.error("查询人口和建筑物概况失败!", e);
//            restResponse = RestResponse.fail("查询人口和建筑物概况失败");
//        }
//        return restResponse;
//    }

    @PostMapping("/deleteHosPeo/{id}")
    public RestResponse deleteHosPeo(@PathVariable("id") String id){

        try {
            hosPeoService .deleteHosPeo(id);
            return ResponseCreator.createSuccessResponse(null,"删除成功");
        } catch (Exception e) {
            log.error("删除失败",e);
            return RestResponse.fail("删除失败");
        }
    }
    @PostMapping("/hosPeoExportExcel")
    public void exportExcel(HttpServletResponse response) throws Exception {
        try {
            // hosPeoService.exportExcel(response);
        log.error("功能暂时缺失");
        } catch (Exception e) {
            log.error("导出失败!",e);
        //     RestResponse.fail("导出失败!");
        }
    }

    @PostMapping("/hosPeoImportExcel")
    public RestResponse importExcel(MultipartFile file)throws IOException {

        try {
            return hosPeoService.hosPeoImportExcel(file);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.fail("导入失败");
        }
    }

    @PostMapping("/cover")
    public RestResponse cover(@RequestBody List<HosPeoEntity> hosPeoEntities){
        RestResponse restResponse = null;
        try {
            hosPeoService.cover(hosPeoEntities);
            restResponse=RestResponse.succeed("成功导入"+hosPeoEntities.size()+"条.");
        } catch (Exception e) {
            log.error("覆盖人口和建筑物概况失败",e);
            restResponse =   RestResponse.fail("覆盖人口和建筑物概况失败");
        }
        return restResponse;
    }

}
